package com.yx.pc.api.service.impl;

import com.yx.common.utils.R;
import com.yx.modules.api.dao.TbAuthorApplyDao;
import com.yx.modules.api.dao.TbUserMapper;
import com.yx.modules.api.entity.TbAuthorApply;
import com.yx.modules.api.entity.TbUser;
import com.yx.pc.api.service.PcTbAuthorApplyService;
import com.yx.system.dao.SysUserDao;
import com.yx.system.dao.SysUserRoleDao;
import com.yx.system.entity.SysUser;
import com.yx.system.service.SysUserRoleService;
import com.yx.system.service.SysUserService;
import org.apache.tomcat.util.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;
import java.util.Map;




@Service
public class PcTbAuthorApplyServiceImpl implements PcTbAuthorApplyService {
	@Autowired
	private TbAuthorApplyDao authorApplyDao;
	@Autowired
	private SysUserRoleDao sysUserRoleDao;
	@Autowired
	private SysUserDao sysUserDao;
	@Autowired
	private TbUserMapper userMapper;
	
	@Override
	public TbAuthorApply queryObject(Integer  id){
		return authorApplyDao.queryObject( id);
	}
	
	@Override
	public List<TbAuthorApply> queryList(Map<String, Object> map){
		return authorApplyDao.queryList(map);
	}
	
	@Override
	public int queryTotal(Map<String, Object> map){
		return authorApplyDao.queryTotal(map);
	}
	
	@Override
	public void save(TbAuthorApply authorApply){
		authorApplyDao.save(authorApply);
	}
	
	@Override
	public void update(TbAuthorApply authorApply){
		authorApplyDao.update(authorApply);
	}
	
	@Override
	public void delete(Integer  id){
		authorApplyDao.delete( id);
	}
	
	@Override
	public void deleteBatch(Integer[]  ids){
		authorApplyDao.deleteBatch( ids);
	}

	@Override
	public R passApply(TbAuthorApply authorApply, SysUser sysUser1) throws UnsupportedEncodingException {

		//根据申请type判断是申请普通作家还是签约作家
		if (authorApply.getType()==0){
			SysUser sysUser = new SysUser();
			sysUser.setCreateTime(new Date());
			sysUser.setCreateUserId(sysUser1.getUserId());
			sysUser.setEmail(authorApply.getEmail());
			sysUser.setMobile(authorApply.getUsername());
			sysUser.setUsername(authorApply.getUsername());
			sysUser.setPassword(authorApply.getPassword());
			sysUser.setIdentity(1);
			sysUser.setStatus(1);
			sysUserDao.insert(sysUser);
			//赋予固定角色权限
			sysUserRoleDao.beauthor(sysUser.getUserId(),4L);
			//改变申请记录状态并更新
			authorApply.setStatus(1);
			String arr = new String(Base64.decodeBase64(authorApply.getText()),"utf-8");
			authorApply.setText(arr);
			authorApplyDao.update(authorApply);
			//改变用户身份并更新
			TbUser tbUser = userMapper.selectByPrimaryKey(Long.valueOf(authorApply.getUserid()));
			if (tbUser==null){
				return R.error("查无此用户");
			}
			tbUser.setIdentity(1);
			tbUser.setAuthorGrade(1);
			userMapper.updateByPrimaryKey(tbUser);
			return R.ok();
		}else if (authorApply.getType()==1){
			SysUser sysUser = sysUserDao.findCommonAuthorByUsername(authorApply.getUsername());
			if (sysUser == null){
				return R.error("系统账号中未查到此申请用户");
			}
			sysUser.setCreateUserId(sysUser1.getUserId());
			sysUser.setIdentity(2);
			sysUserDao.updateById(sysUser);
			//改变申请记录状态并更新
			authorApply.setStatus(1);
			authorApplyDao.update(authorApply);
			//改变用户身份并更新
			TbUser tbUser = userMapper.selectByPrimaryKey(Long.valueOf(authorApply.getUserid()));
			if (tbUser==null){
				return R.error("查无此用户");
			}
			tbUser.setIdentity(2);
			tbUser.setAuthorGrade(2);
			userMapper.updateByPrimaryKey(tbUser);
			return R.ok();
		}
		return R.error();
	}


	@Override
	public R refuseApply(TbAuthorApply authorApply, SysUser sysUser1) throws UnsupportedEncodingException {
			//拒绝改变申请状态
			authorApply.setStatus(2);
			if (authorApply.getType()==0){
				String arr = new String(Base64.decodeBase64(authorApply.getText()),"utf-8");
				authorApply.setText(arr);
			}
			authorApplyDao.update(authorApply);
			System.out.println(authorApply.getRefuseReason());
			return R.ok();
	}

	@Override
	public List<TbAuthorApply> findAllByUser(Integer userId) {
		return authorApplyDao.findAllByUser(userId);
	}

	@Override
	public List<TbAuthorApply> findAllBySysUser(Integer userId) {
		return authorApplyDao.findAllBySysUser(userId);
	}

}
